// PIC18F14K50 jiba 14k50
// FileName: jiba 14k50



#include <p18f14k50.h>
#include <delays.h>
#include <timers.h>
#include "lib_adc.h"


#pragma config CPUDIV=NOCLKDIV //CPUデバイダ÷1
#pragma config USBDIV = OFF //USBデバイダ÷1
#pragma config FOSC = HS //外部クロック使用
#pragma config PLLEN = ON //PLL×4
#pragma config PCLKEN = OFF
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF,BOREN = OFF
#pragma config WDTEN = OFF
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF
#pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF
#pragma config WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF
#pragma config BORV = 30
#pragma config WDTPS = 32768

#define lcd_DB LATB
#define lcd_E LATCbits.LATC3
#define lcd_RS LATCbits.LATC4

#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)

unsigned char i;
unsigned int b1, b2, N;

void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);

void lcd_putN(unsigned int N);

void UserInit(void);
void lcd_Init(void);

void high(void);
void low(void);


#pragma interrupt high
#pragma interruptlow low
#pragma code H_vect = 0x0008
void H_isr(void){
_asm goto high _endasm
}
#pragma code L_vect = 0x0018
void L_isr(void){
_asm goto low _endasm
}
#pragma code



void high(){
}

void low(){
}



void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1;
Delay1TCY();
lcd_E = 0;
}

void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}

void lcd_clr(void){
lcd_c4(0x01);
}

void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}

void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}

void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}




void lcd_putN(unsigned int N){
unsigned char buf[4];

for(i=0; i<4; i++) buf[i] = ' ';

i=3;
do{
buf[i]=(N%10)+'0';
N=N/10;
i--;
} while(N>0);
for(i=0; i<4; i++) lcd_putchr(buf[i]);
}





void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}

void UserInit(void){
b1 = 0; b2 = 0; N = 0;
}

char lineG[] = "GaussMeter";
char BB[] = "B= ";
char minus[] = "-";
char GG[] = "(G)";
char blank[] = " ";




void main(void){

PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b11000001; // AN8,9,VREF+入力
ANSEL = 0b00000000;
ANSELH = 0b00000011; // AN8,9 アナログ入力


INTCON3bits.INT1IE = 0;//割り込みなし
INTCON3bits.INT1IP = 0;// 低優先
INTCON3bits.INT1IF = 0;// フラグ


lcd_init();
UserInit();


while(1){

Delay_20mS;

OpenADC(ADC_FOSC_4 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VREFPLUS &
ADC_REF_VDD_VSS,
ADC_CH9 &
ADC_INT_OFF );

ConvertADC();
while(BusyADC());
b1=(ADRESH*256) + ADRESL;

Delay_20mS;

OpenADC(ADC_FOSC_4 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VREFPLUS &
ADC_REF_VDD_VSS,
ADC_CH8 &
ADC_INT_OFF );

ConvertADC();
while(BusyADC());
b2=(ADRESH*256) + ADRESL;

if(b1 < 32) b1 = 0; // 端数は0にする
if(b2 < 32) b2 = 0;

N = b1;

if(b1 < b2) N = b2;

N = N * 2; // そのままガウス単位とする


lcd_clr();
lcd_locate(0,0);
lcd_puts(blank);
lcd_puts(lineG);
lcd_puts(blank);
lcd_puts(GG);

lcd_locate(1,0);
lcd_puts(blank);lcd_puts(blank);
lcd_puts(blank);lcd_puts(blank);
lcd_puts(BB);
lcd_puts(blank);
if(b1 > b2) lcd_puts(blank);
if(b1 < b2) lcd_puts(minus); // マイナス符号をつける
lcd_putN(N);


Delay_100mS;Delay_100mS;


}
}




                   戻る